package com.sdu.cloudservice.dao.impl;

import com.sdu.cloudservice.dao.SpiderHistoryDao;
import com.sdu.cloudservice.entity.SpiderHistoryEntity;
import com.sdu.cloudservice.entity.SpiderHistoryEntityCriteria;
import com.sdu.cloudservice.mapper.SpiderHistoryMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * Create by zekdot on 19-6-25.
 */
@Repository
public class SpiderHistoryDaoImpl implements SpiderHistoryDao {

    @Resource
    private SpiderHistoryMapper spiderHistoryMapper;
    @Override
    public int insertHistory(SpiderHistoryEntity spiderHistoryEntity) {
        return spiderHistoryMapper.insert(spiderHistoryEntity);
        //return 0;
    }

    @Override
    public List<SpiderHistoryEntity> getHistoryByPage(int page) {
        SpiderHistoryEntityCriteria spiderHistoryEntityCriteria = new SpiderHistoryEntityCriteria();
        SpiderHistoryEntityCriteria.Criteria criteria = spiderHistoryEntityCriteria.createCriteria();
        criteria.andIdGreaterThan(0);
        spiderHistoryEntityCriteria.setOrderByClause("id desc");
        spiderHistoryEntityCriteria.setLimitNum((page - 1)*onePage,onePage);
        spiderHistoryEntityCriteria.or(criteria);
        List<SpiderHistoryEntity> list = spiderHistoryMapper.selectByExample(spiderHistoryEntityCriteria);

        return list;
    }

    /**
     * 获取页码总数
     *
     * @return
     */
    @Override
    public int getPageNumber() {
        SpiderHistoryEntityCriteria spiderHistoryEntityCriteria = new SpiderHistoryEntityCriteria();
        SpiderHistoryEntityCriteria.Criteria criteria = spiderHistoryEntityCriteria.createCriteria();
        criteria.andIdGreaterThan(0);
        spiderHistoryEntityCriteria.or(criteria);
        long num = spiderHistoryMapper.countByExample(spiderHistoryEntityCriteria);
        return (int)num;
    }

    @Override
    public SpiderHistoryEntity getLastHistory() {
        SpiderHistoryEntityCriteria spiderHistoryEntityCriteria = new SpiderHistoryEntityCriteria();
        spiderHistoryEntityCriteria.setLimitNum(0,1);
        spiderHistoryEntityCriteria.setOrderByClause("id desc");
        List<SpiderHistoryEntity> list = spiderHistoryMapper.selectByExample(spiderHistoryEntityCriteria);
        if(list == null || list.isEmpty()){
            return null;
        }
        return list.get(0);
    }
}
